﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using YTE.Control;
using 右下角弹窗.DiagShow;

namespace LD.BC
{
    public partial class FrmAxisSet : FrmBase
    {
        private TB_BC _bc;
        public FrmAxisSet()
        {
            InitializeComponent();
            this.Shown += FrmAxisSet_Shown;
        }
        public FrmAxisSet(TB_BC bc) : this()
        {
            _bc = bc;
        }
        private void FrmAxisSet_Shown(object sender, EventArgs e)
        {
            UI();
            Init();
        }

        private void 删除此行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = this.dgv_data.SelectedRows;
            if (rows.Count > 0)
            {
                this.dgv_data.Rows.Remove(rows[0]);
            }

            this.dgv_data.ContextMenuStrip = null;
        }
        private void dgv_data_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            if (index > -1)
            {
                var row = this.dgv_data.SelectedRows[0];
                int axisId = Convert.ToInt32(row.Cells["AxisId"].Value);
                var axis = _bc.AxisParam.Where(p => p.AxisId == axisId).FirstOrDefault();
                this.txt_name.Text = axis.Name;
                this.num_axisId.Value = axis.AxisId;
                this.num_curVal.Value = (decimal)axis.Current_val;
                this.chk_isEnabled.Checked = axis.IsEnabled;

                this.num_manualSpeek.Value = (decimal)axis.Manual_val;
                this.num_startSpeek.Value = (decimal)axis.Min_val;
                this.num_maxSpeek.Value = (decimal)axis.Max_val;
                this.num_stopSpeek.Value = (decimal)axis.Stop_val;
                this.num_tacc.Value = (decimal)axis.Tacc;
                this.num_tdec.Value = (decimal)axis.Tdec;
                this.num_zeroSpeek.Value = (decimal)axis.Zero_val;
                cob_zeroMode1.SelectedIndex = axis.Zero_Model;
                cob_zeroDir1.SelectedIndex = axis.Zero_dir;
                cob_plusMode1.SelectedIndex = axis.Plus_Model;
            }
        }
        private void dgv_data_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.dgv_data.ContextMenuStrip = this.contextMenuStrip1;
        }
        private void dgv_data_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > 0 && e.ColumnIndex <= 9)
            {
                Console.WriteLine("数据格式检查");
                decimal result;
                if (this.dgv_data[e.ColumnIndex, e.RowIndex].Value == null)
                    return;
                if (!decimal.TryParse(this.dgv_data[e.ColumnIndex, e.RowIndex].Value.ToString(), out result))
                {
                    Dialog dialog = new Dialog();
                    dialog.Warning("数据格式错误");
                    this.dgv_data.CurrentCell = this.dgv_data[e.ColumnIndex, e.RowIndex];
                    this.dgv_data[e.ColumnIndex, e.RowIndex].Selected = true;
                    this.dgv_data[e.ColumnIndex, e.RowIndex].Value = 0;
                }
            }
        }

        private void UI()
        {
            DataTable zeroDir = GetZeroDir();
            DataTable zeroMode = GetZeroMode();
            DataTable plusMode = GetPlusMode();
            this.cob_zeroDir.DataSource = zeroDir;
            this.cob_zeroDir.DisplayMember = "name";
            this.cob_zeroDir.ValueMember = "id";

            this.cob_zeroMode.DataSource = zeroMode;
            this.cob_zeroMode.DisplayMember = "name";
            this.cob_zeroMode.ValueMember = "id";

            this.cob_plusMode.DataSource = plusMode;
            this.cob_plusMode.DisplayMember = "name";
            this.cob_plusMode.ValueMember = "id";

            this.cob_zeroDir1.DataSource = zeroDir;
            this.cob_zeroDir1.DisplayMember = "name";
            this.cob_zeroDir1.ValueMember = "id";

            this.cob_zeroMode1.DataSource = zeroMode;
            this.cob_zeroMode1.DisplayMember = "name";
            this.cob_zeroMode1.ValueMember = "id";

            this.cob_plusMode1.DataSource = plusMode;
            this.cob_plusMode1.DisplayMember = "name";
            this.cob_plusMode1.ValueMember = "id";

        }
        private DataTable GetZeroDir()
        {
            string[] str = { "负方向", "正方向" };
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < str.Length; i++)
            {
                dt.Rows.Add(i, str[i]);
            }
            return dt;
        }
        private DataTable GetZeroMode()
        {
            string[] str = { "一次回零", "一次回零+反找", "二次回零", "一次回零+EZ", "EZ回零", "一次回零+反找EZ", "原点锁存", "原点+EZ锁存", "EZ锁存", "原点+反向EZ锁存", "限位一次回零", "限位回零反找", "限位二次回零" };
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < str.Length; i++)
            {
                dt.Rows.Add(i, str[i]);
            }
            return dt;
        }
        private DataTable GetPlusMode()
        {
            string[] str = { "脉冲高+方向高", "脉冲低+方向高", "脉冲高+方向低", "脉冲低 + 方向低", "双脉冲高", "双脉冲低", "AB相" };
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            for (int i = 0; i < str.Length; i++)
            {
                dt.Rows.Add(i, str[i]);
            }
            return dt;
        }
        public void New()
        {
            this.dgv_data.Rows.Add();
        }


        public void Save()
        {
            List<TB_AxisParam> axisParam = new List<TB_AxisParam>();
            foreach (DataGridViewRow item in this.dgv_data.Rows)
            {
                //object b = item.Cells[11].EditedFormattedValue;
                axisParam.Add(new TB_AxisParam()
                {
                    AxisId = Convert.ToUInt16(item.Cells["AxisId"].Value),
                    Name = item.Cells["AxisName"].Value.ToString(),
                    Min_val = Convert.ToDouble(item.Cells["Min_val"].Value),
                    Manual_val = Convert.ToDouble(item.Cells["Manual_val"].Value),
                    Max_val = Convert.ToDouble(item.Cells["Max_val"].Value),
                    Stop_val = Convert.ToDouble(item.Cells["Stop_val"].Value),
                    Tacc = Convert.ToDouble(item.Cells["Tacc"].Value),
                    Tdec = Convert.ToDouble(item.Cells["Tdec"].Value),
                    //S_Para = Convert.ToDouble(item.Cells["S_Para"].Value),
                    Zero_val = Convert.ToDouble(item.Cells["Zero_val"].Value),
                    Current_val = Convert.ToDouble(item.Cells["Current_val"].Value),
                    Zero_Model = Convert.ToInt32(item.Cells["cob_zeroMode"].Value),
                    Zero_dir = Convert.ToInt32(item.Cells["cob_zeroDir"].Value),
                    Plus_Model = Convert.ToInt32(item.Cells["cob_plusMode"].Value),
                    //PartNoId = BcFactory.PartNoId,
                });
            }

            SaveDB(axisParam);
        }
        private void SaveDB(List<TB_AxisParam> axisParam)
        {
            //AxisParamDal axisDal = new AxisParamDal();
            ////delete old
            ////axisDal.Delete(BcFactory.PartNoId);
            //int result = axisDal.Insert(axisParam);
            //Dialog dialog = new Dialog();
            //if (result > 0)
            //    dialog.Success("保存完成");
            //else
            //    dialog.Warning("保存失败");
            //BcFactory.AxisParamList = axisDal.GetList(BcFactory.PartNoId);
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            int axisId = (int)this.num_axisId.Value;
            var isExist = _bc.AxisParam.Where(p => p.AxisId == axisId).FirstOrDefault();
            if (isExist == null)
            {
                _bc.AxisParam.Add(new TB_AxisParam()
                {
                    Name = this.txt_name.Text,
                    AxisId = (ushort)axisId,
                    IsEnabled = this.chk_isEnabled.Checked,
                    Current_val = (double)this.num_curVal.Value,

                    Manual_val = (double)this.num_manualSpeek.Value,
                    Min_val = (double)this.num_startSpeek.Value,
                    Max_val = (double)this.num_maxSpeek.Value,
                    Stop_val = (double)this.num_stopSpeek.Value,
                    Tacc = (double)this.num_tacc.Value,
                    Tdec = (double)this.num_tdec.Value,

                    Zero_val = (double)this.num_zeroSpeek.Value,
                    Zero_Model = cob_zeroMode1.SelectedIndex,
                    Zero_dir = cob_zeroDir1.SelectedIndex,
                    Plus_Model = cob_plusMode1.SelectedIndex,
                });

                this.num_axisId.Value++;
            }
            else
            {
                isExist.AxisId = (ushort)axisId;
                isExist.IsEnabled = this.chk_isEnabled.Checked;
                isExist.Current_val = (double)this.num_curVal.Value;

                isExist.Manual_val = (double)this.num_manualSpeek.Value;
                isExist.Min_val = (double)this.num_startSpeek.Value;
                isExist.Max_val = (double)this.num_maxSpeek.Value;
                isExist.Stop_val = (double)this.num_stopSpeek.Value;
                isExist.Tacc = (double)this.num_tacc.Value;
                isExist.Tdec = (double)this.num_tdec.Value;

                isExist.Zero_val = (double)this.num_zeroSpeek.Value;
                isExist.Zero_Model = cob_zeroMode1.SelectedIndex;
                isExist.Zero_dir = cob_zeroDir1.SelectedIndex;
                isExist.Plus_Model = cob_plusMode1.SelectedIndex;
            }
            BcFactory.Instance.Save();
            Init();

            BcFactory.Instance?.Control.DownAxisParam();
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            int axisId = (int)this.num_axisId.Value;
            var isExist = _bc.AxisParam.Where(p => p.AxisId == axisId).FirstOrDefault();
            BcFactory.Instance.Save();
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定要删除所选信息", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                var isExist = _bc.AxisParam.Where(p => p.AxisId == (ushort)this.num_axisId.Value).FirstOrDefault();
                if (isExist != null)
                {
                    _bc.AxisParam.Remove(isExist);
                    var row = this.dgv_data.SelectedRows[0];
                    this.dgv_data.Rows.Remove(row);
                    BcFactory.Instance.Save();
                }
            }
        }
        private void Init()
        {
            this.dgv_data.Rows.Clear();
            foreach (var item in _bc.AxisParam)
            {
                int index = this.dgv_data.Rows.Add();

                this.dgv_data["AxisId", index].Value = item.AxisId;
                this.dgv_data["AxisName", index].Value = item.Name;
                this.dgv_data["Min_val", index].Value = item.Min_val;               //起始速度
                this.dgv_data["Manual_val", index].Value = item.Manual_val;         //手动速度
                this.dgv_data["Max_val", index].Value = item.Max_val;               //最大速度
                this.dgv_data["Stop_val", index].Value = item.Stop_val;             //终止速度
                this.dgv_data["Tacc", index].Value = item.Tacc;                     //加速时间
                this.dgv_data["Tdec", index].Value = item.Tdec;                     //减速时间
                this.dgv_data["Zero_val", index].Value = item.Zero_val;             //回零速度
                this.dgv_data["Current_val", index].Value = item.Current_val;       //当量
                this.dgv_data["IsEnabled", index].Value = item.IsEnabled;           //有效否

                DataGridViewComboBoxCell cobCell10 = this.dgv_data["cob_zeroMode", index] as DataGridViewComboBoxCell;
                cobCell10.Value = item.Zero_Model + "";                             //回零模式

                DataGridViewComboBoxCell cobCell11 = this.dgv_data["cob_zeroDir", index] as DataGridViewComboBoxCell;
                cobCell11.Value = item.Zero_dir + "";                               //回零方式

                DataGridViewComboBoxCell cobCell12 = this.dgv_data["cob_plusMode", index] as DataGridViewComboBoxCell;
                cobCell12.Value = item.Plus_Model + "";                             //脉冲模式
            }
            this.txt_name.Text = string.Empty;
        }
    }
}
